package com.zsc.newbuds.controller.admin;
import java.util.*;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.RecyclingArea;
import com.zsc.newbuds.service.impl.AdminServiceImpl;
import com.zsc.newbuds.service.impl.RecyclingAreaServiceImpl;
import com.zsc.newbuds.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author D
 */
@RequestMapping("admin/recyclingServices")
@Controller
public class RecyclingServicesController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AdminServiceImpl adminService;
    @Autowired
    RecyclingAreaServiceImpl recyclingAreaService;

    @ResponseBody
    @GetMapping("/getRecyclingAreaList")
    public List<RecyclingArea> getRecyclingAreaList(){
        return recyclingAreaService.getAreaList();
    }

    /**
     * 匹配地区的工作人员
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/getAreaAdminList")
    public List<Admin> getAreaAdminList(@RequestBody Map<String, Object> params){

        String city = (String) params.get("city");
        System.out.println(city);
        List<Admin> list = new ArrayList<>();
        List<Admin> adminList = adminService.getAdminList();
        System.out.println(adminList);
        for(Admin admin:adminList){
            if(admin.getAddress().equals(city)&& "未分配".equals(admin.getWorkArea())){
                list.add(admin);
                System.out.println(admin+"匹配到的工作人员");
            }
        }
        return list;
    }

    @ResponseBody
    @GetMapping("/getAdminList")
    public List<Admin> getAdminList(){
        List<Admin> admins = new ArrayList<>();
        for(Admin admin : adminService.getAdminList()){
            if(!"admin".equals(admin.getPosition())){
                admins.add(admin);
            }
        }
        return admins;
    }

    @ResponseBody
    @PostMapping("/assignAdmin")
    public int assignAdmin(String[] workId,String [] workName,String workArea,Integer areaId){
        recyclingAreaService.updateRecyclingAreaState(workId, areaId,"已分配",workName);
        return 1;
    }
}
